package com.oamicnet;

import com.oamicnet.entity.Student;
import com.oamicnet.proxy.MyInvocationHandler;
import com.oamicnet.service.StudentService;
import com.oamicnet.service.StudentServiceImpl;

import java.lang.reflect.Proxy;

/**
 * Hello world!
 *
 */
public class JDKProxyApp
{
    public static void main( String[] args )
    {
        Student student = new Student();
        student.setId(1);
        student.setName("John");
        student.setEmail("oamicnet@gmail.com");

        // 原始对象
        StudentService studentService = new StudentServiceImpl();

//        studentService.save(student);

        // 生成代理对象
        StudentService proxy = (StudentService) Proxy.newProxyInstance(
                studentService.getClass().getClassLoader(),
                studentService.getClass().getInterfaces(),
                new MyInvocationHandler(studentService)
        );
        // 调用代理对象的方法
        proxy.save(student);

        proxy.update(student);
    }
}
